#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <unistd.h>
#include <pthread.h>

using namespace std;

string changeId(const pthread_t &thread_id)
{    
    char tid[128];
    snprintf(tid, sizeof(tid), "0x%x", thread_id);
    return tid;
}

void *start_routine(void *args)
{
    string threadname = static_cast<const char*>(args);
    int cnt = 5;
    while(cnt--)
    {
        cout << threadname << " running ..." << changeId(pthread_self()) << endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, start_routine, (void*)"thread one");
    
    pthread_detach(pthread_self());
    
    string main_id = changeId(pthread_self());
    cout << "main thread running ... new thread id: " << changeId(tid) << " main thread id: " << main_id << endl;

    sleep(5);
    //int n = pthread_join(tid, nullptr);
    //if(n < 0) cerr << "errno: " << errno << strerror(errno) << endl;
    //cout << "result: " << n << strerror(n) << endl;
    return 0;
}